const store = {
  policy: {}
}

// put : yield put({type:'global/setUser', payload:{name:'wrh'}})
// put : yield put({type:'load', payload:{name:'wrh'}})
// call : yield call(fn, fnParams,...args)
// select:  yield select((store)=>store.policy)
// take:
function f() {
  return Promise.resolve({});
}

// 渲染树需要什么
// 1. 一个标题
// 2. 一个标识
// 可选
// 3. 附加数据


export default {
  namespace: 'policy',
  state: {
    treeData: [],
    attachList: [],
    selectedKeys: [],
    sum: 0,
    total: 0,
    page: 1,
    pageSize: 5,
    currentType: null,
    searchKeywords: '',
  },
  effects: {
    * loadTreeData({payload}, {put, call}) {
      const res = yield call(f);
      // 转换数据结构
      const data = [
        {
          id: 120,
          title: '国家标准',
          data: {},
        }
      ];
      if (!data.length) {
        return;
      }
      const [{data: {code}}] = data[0];
      yield put({treeData: data, currentType: code})
      yield put({type: 'loadAttachList'});
    },
    * loadAttachList({payload}, {put, call, select}) {
      const {page: paramsPage, pageSize: paramPageSize} = payload || {};
      const {page, pageSize, searchKeywords: title, currentType: typeCode} = yield select(store => store.policy);
      const res = yield call(f(), {typeCode, pageSize: paramPageSize || pageSize, page: paramsPage || page, title})
      // 转换数据结构
      const data = [
        {
          id: 11,
          title: '',
          icon: '',
          uploadTime: '2020-03-04',
        }
      ]
      yield put({type: 'load', payload: {attachList: data}})
    },
    * changePage({payload}, {put}) {
      const {page, pageSize} = payload;
      yield put({type: 'load', payload: {page, pageSize}})
      yield put({type: 'loadAttachList', payload: {page, pageSize}})
    },
    * changeAttachType({payload}, {put}) {
      const {code, selectedKeys} = payload;
      yield put({type: 'load', payload: {currentType: code, selectedKeys}})
    },
  },
  reducers: {
    load(state, {payload}) {
      return {
        ...state,
        ...payload,
      }
    }
  }

}


@connect(({policy}) => {
  return {
    treeData: policy.treeData,
    selectedKeys: policy.selectedKeys,
  }
}, (dispatch) => {
  return {
    changeAttachType(code, selectedKeys) {
      return dispatch({type: 'policy/changeAttachType', payload: {code, selectedKeys}});
    }
  }
})
class App extends React.Component {
  static propTypes = {};

  componentDidMount() {
    this.props.boot();
  }

  handleSelect = (selectedKeys, e) => {
    const item = e.node.props.dataRef;
    const {code, selectedKeys} = item;
    this.props.changeAttachType(code, selectedKeys);
  }

  render() {
    const {treeData, selectedKeys} = this.props;
    return (
      <div className="app">
        <h2 className='title'>
          <span className="text">Boot....</span>
        </h2>
        <Button status='pure' active>Start</Button>
      </div>
    );
  }
}

export default App;
